package cn.epsilon3.media.oss.aliyun.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectResult;
import cn.epsilon3.configBean.InitializingAliyunOssConfig;
import cn.epsilon3.utils.SpringContextUtils;
import cn.epsilon3.media.oss.aliyun.service.AliyunOssClient;
import cn.epsilon3.media.oss.aliyun.service.AliyunOssHandler;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * 阿里云oss服务器
 *
 * @author epsilon3
 * @date 2023/07/14
 */
@Service
public class AliyunOssClientImpl implements AliyunOssClient {

    private static final Log log = LogFactory.get();

    @Resource
    InitializingAliyunOssConfig ossConfig;


    @Override
    public void uploadFile(String dirName, MultipartFile[] multipartFiles) {
        try {
            if (ArrayUtil.isEmpty(multipartFiles)) {
                log.error("multipartFiles is empty");
            } else {
                OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(),
                        ossConfig.getAccessKeyId().trim(),
                        ossConfig.getAccessKeySecret().trim());
                String bucketName = ossConfig.getBucketName();
                if (! ossClient.doesBucketExist(bucketName)) {
                    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                    createBucketRequest.setLocationConstraint(ossConfig.getEndpoint());
                    createBucketRequest.setCannedACL(CannedAccessControlList.PublicReadWrite);
                    ossClient.createBucket(createBucketRequest);
                    log.info("create " + bucketName + " success!" );
                }
                for (MultipartFile multipartFile : multipartFiles) {
                    String name = multipartFile.getOriginalFilename();
                    File file = new File(name);
                    FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
                    String fileName = file.getName();
                    String fileKey = dirName + "/" + fileName;
                    AliyunOssHandler bean = SpringContextUtils.getBean(AliyunOssHandler.class);
                    if (ObjectUtil.isNotNull(bean)) {
                        bean.preProcess(dirName,fileKey,fileName);
                    }
                    PutObjectResult putObjectResult = ossClient.putObject(bucketName, fileKey, file);
                    file.delete();
                    if (ObjectUtil.isNotNull(bean)) {
                        bean.postProcess(putObjectResult);
                    }
                    log.info("file " + name + " upload success!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
